﻿using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.IDAO;
using CNKI.TPI.Web.Search.Model;
using KNet.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CNKI.TPI.Web.Search.DAO
{
    public class HomeDAO : BaseDAO, IHomeDAO
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> GetHomepageConfig(PermissionOption option = PermissionOption.FilteringPermissions)
        {
            IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> result = new List<SYS_CMS_NEWS_NEWSCONTENTCONFIG>();
            try
            {
                ExpressionCombine<SYS_CMS_NEWS_NEWSCONTENTCONFIG> exp = new ExpressionCombine<SYS_CMS_NEWS_NEWSCONTENTCONFIG>();
                if (SessionHelper.IsUserInfoExsit() && option == PermissionOption.FilteringPermissions)
                {
                    IList<int> userDBInfo = SessionHelper.GetUserDBRight();
                    if (userDBInfo != null && userDBInfo.Count > 0)
                    {
                        foreach (int item in userDBInfo)
                        {
                            exp.Or("DbID", OperatorType.Equal, item);
                        }
                    }
                }
                result = Context.Get<SYS_CMS_NEWS_NEWSCONTENTCONFIG>(exp.Compile());
                if (result != null)
                {
                    result = result.OrderBy(c => c.SortNo).ToList();
                }
            }
            catch(Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_NEWS_JUMPTEMPLATE> GetHomepageJumpConfig(PermissionOption option = PermissionOption.FilteringPermissions)
        {
            IList<SYS_CMS_NEWS_JUMPTEMPLATE> result = new List<SYS_CMS_NEWS_JUMPTEMPLATE>();
            try
            {
                ExpressionCombine<SYS_CMS_NEWS_JUMPTEMPLATE> exp = new ExpressionCombine<SYS_CMS_NEWS_JUMPTEMPLATE>();
                if (SessionHelper.IsUserInfoExsit() && option == PermissionOption.FilteringPermissions)
                {
                    IList<int> userDBInfo = SessionHelper.GetUserDBRight();
                    if (userDBInfo != null && userDBInfo.Count > 0)
                    {
                        foreach (int item in userDBInfo)
                        {
                            exp.Or("DbID", OperatorType.Equal, item);
                        }
                    }
                }
                result = Context.Get<SYS_CMS_NEWS_JUMPTEMPLATE>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
    }
}
